package com.mz.ozy.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mz.ozy.R;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import java.util.List;

/**
 * Created by sqzglg on 2017/10/10.
 * 仿微信朋友圈图片展示
 */

public class FriendsCircleImageLayout extends ViewGroup {
    /**
     * 显示的行数
     */
    private int mColumnCount;
    /**
     * 默认间距
     */
    private final int DEFAULT_SPACING = 10;
    private float mSpacing;
    /**
     * 图片宽高比(党为多张图片的时候为1)
     * 一般在url中会有宽高 可计算
     */
    private float mItemAspectRatio;
    /**
     * 最宽的时候相对可用空间比例（当childCount=1的时候）
     * 当只有一张图片的 最大显示宽度和高度相对于可用宽的的比例
     */
    private final float MAX_WIDTH_PERCENTAGE = 270f / 350;

    private int mItemWidth;
    private int mItemHeight;


    public FriendsCircleImageLayout(Context context) {
        this(context, null);
    }

    public FriendsCircleImageLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FriendsCircleImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Friends_Circle_Image_Layout);
        mSpacing = array.getDimensionPixelOffset(R.styleable.Friends_Circle_Image_Layout_image_space, DEFAULT_SPACING);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int count = getChildCount();
        final int width = MeasureSpec.getSize(widthMeasureSpec);

        //当只有一张图片的时候 显示一张相对比较大的图片
        //当大于1小于等于4张图片的时候 一排显示两张图片

        //当只有一张图片的时候
        if (count == 1) {
            mColumnCount = 1;
//            int mItemMaxWidth = width;
//            int mItemMaxHeight = mItemMaxWidth;
//            if (mItemAspectRatio < 1) {
//                mItemHeight = mItemMaxHeight;
//                mItemWidth = (int) (mItemHeight * mItemAspectRatio);
//            } else {
//                mItemWidth = mItemMaxWidth;
//                mItemHeight = (int) (mItemMaxWidth / mItemAspectRatio);
//            }

            mItemWidth = width;
            mItemHeight = mItemWidth;
        } else {
            if (count <= 2) {
                mColumnCount = 2;
                mItemWidth = (int) ((width - getPaddingLeft() - getPaddingRight() - mSpacing) / 2);
                mItemHeight = mItemWidth;
            } else {
                mColumnCount = 3;
                mItemWidth = (int) ((width - getPaddingLeft() - getPaddingRight() - 2 * mSpacing) / 3);
                mItemHeight = mItemWidth;
            }
        }


        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.width = mItemWidth;
            layoutParams.height = mItemHeight;
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    getDesiredHeight(mItemHeight), MeasureSpec.EXACTLY);
        }

//        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
//            widthMeasureSpec = MeasureSpec.makeMeasureSpec(
//                    getDesiredWidth(mItemWidth), MeasureSpec.EXACTLY);
//        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec,
                                int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();
        //获取子控件的宽高约束规则
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                getPaddingLeft() + getPaddingRight(), lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                getPaddingLeft() + getPaddingRight(), lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

    private int getDesiredHeight(int mItemHeight) {
        int totalHeight = getPaddingTop() + getPaddingBottom();
        int count = getChildCount();
        if (count > 0) {
            int row = (count - 1) / mColumnCount;
            totalHeight = (int) ((row + 1) * mItemHeight + (row) * mSpacing) + totalHeight;
        }
        return totalHeight;
    }

//    private int getDesiredWidth(int mItemWidth) {
//        int totalWidth = getPaddingLeft() + getPaddingRight();
//        int count = getChildCount();
//        if (count > 0) {
//            if (count < mColumnCount) {
//                totalWidth = (int) (count * mItemWidth + (count - 1) * mSpacing) + totalWidth;
//            } else {
//                totalWidth = (int) (count * mItemWidth + (count - 1) * mSpacing) + totalWidth;
//            }
//
//        }
//        return totalWidth;
//    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View imageView = getChildAt(i);

            int column = i % mColumnCount;
            int row = i / mColumnCount;
            int left = (int) (getPaddingLeft() + column * (mSpacing + mItemWidth));
            int top = (int) (getPaddingTop() + row * (mSpacing + mItemHeight));

            imageView.layout(left, top, left + mItemWidth, top + mItemHeight);
        }
    }


    /**
     * 显示图片
     */
//    public void setImageUrls(final List<String> imageUrls) {
//        removeAllViews();
////        if (AppUtil.isEmpty(imageUrls)) {
////            return;
////        }
//
//        int count = imageUrls.size();
//        if (count == 1) {
//            //一般在url中会有宽高 可以算出宽高比
//            //测试url固定用的 1000:1376
////            mItemAspectRatio = AppUtil.getAspectRatio(imageUrls.get(0));
//            mItemAspectRatio = 1000 / 1376f;
//        } else {
//            mItemAspectRatio = 1;
//        }
//
////        for (int i = 0; i < imageUrls.size(); i++) {
////            AppImageView imageView = new AppImageView(getContext());
////            imageView.setImageURI(imageUrls.get(i));
////            addView(imageView);
////            //点击查看大图
////            imageView.setOnClickListener(new OnPhotoListener() {
////                @Override
////                public void onClick(View v) {
////
////                }
////            });
////        }
//    }

    /**
     * 显示图片
     */
    public void setImageUrls(final List<String> imageUrls, onImageListener listener) {
        removeAllViews();
//        if (AppUtil.isEmpty(imageUrls)) {
//            return;
//        }

        for (int i = 0; i < imageUrls.size(); i++) {
            CustomRoundAngleImageView imageView = (CustomRoundAngleImageView) View.inflate(getContext(),
                    R.layout.custom_round_angle_imageview, null);

            final String picUrl = imageUrls.get(i);
            if (TextUtils.isEmpty(picUrl)) {
                GlideUtils.loadImg(getContext(), imageView, R.mipmap.default_foods_icon);
            } else {
                GlideUtils.loadBackImg(getContext(), imageView, picUrl, R.mipmap.default_foods_icon);
            }
//            imageView.setImageResource(imageUrls.get(i));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            addView(imageView);
            //点击查看大图
            final int curPos = i;

            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(curPos);
                    }
                }
            });
        }
    }

    public int getItemWidth() {
        return mItemWidth;
    }

    public int getItemHeight() {
        return mItemHeight;
    }

    public int getColumnCount() {
        return mColumnCount;
    }

    public void setColumnCount(int columnCount) {
        mColumnCount = columnCount;
        invalidate();
    }

    public float getSpacing() {
        return mSpacing;
    }

    public void setSpacing(float spacing) {
        mSpacing = spacing;
        invalidate();
    }

    public float getItemAspectRatio() {
        return mItemAspectRatio;
    }

    public void setItemAspectRatio(float itemAspectRatio) {
        mItemAspectRatio = itemAspectRatio;
    }

    public interface onImageListener {
        void onClick(int pos);
    }
}